<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>Arr Class - Fuel Documentation</title>
	<link href="../assets/css/main.css" media="screen" rel="stylesheet" />
	<script type="text/javascript" src="../assets/js/jquery-1.4.4.min.js"></script>
	<script type="text/javascript" src="../assets/js/nav.js"></script>
	<script type="text/javascript" src="../assets/js/highlight.pack.js"></script>
	<script type="text/javascript">
		$(function() {
			show_nav('classes', '../');
		});
		hljs.tabReplace = '    ';
		hljs.initHighlightingOnLoad();
	</script>
</head>
<body>

	<header>
		<h1>FUEL Documentation</h1>
	</header>

	<div id="main-nav"></div>

	<section id="content">
		<h2>Arr Class</h2>

		<p>The arr class is a set of helper functions for working with arrays.</p>

		<article>
			<h4>flatten_assoc($array, $glue = ':', $reset = true)</h4>
			<p>The <strong>flatten_assoc</strong> method flattens a multi-dimensional associative array down into a 1 dimensional associative array.</p>
			<table class="method">
				<tbody>
				<tr>
					<th class="legend">Static</th>
					<td>Yes</td>
				</tr>
				<tr>
					<th>Parameters</th>
					<td>
						<table class="parameters">
							<tr>
								<th>Param</th>
								<th>Default</th>
								<th class="description">Description</th>
							</tr>
							<tr>
								<th><kbd>$array</kbd></th>
								<td><i>Required</i></td>
								<td>The key in the $_COOKIE array.</td>
							</tr>
							<tr>
								<th><kbd>$glue</kbd></th>
								<td><pre class="php"><code>:</code></pre></td>
								<td>The string used to glue the keys together with</td>
							</tr>
							<tr>
								<th><kbd>$reset</kbd></th>
								<td><pre class="php"><code>true</code></pre></td>
								<td>Should we create a new array of values instead of merging them into the last array created by <strong>flatten_assoc</strong>?</td>
							</tr>
						</table>
					</td>
				</tr>
				<tr>
					<th>Returns</th>
					<td>array</td>
				</tr>
				<tr>
					<th>Example</th>
					<td>
<pre class="php"><code>$people = array(
	array(
		"name" => "Jack",
		"age" => 21
	),
	array(
		"name" => "Jill",
		"age" => 23
	)
);

print_r( arr::flatten_assoc($people) );

// Returns
Array
(
    [0:name] => Jack
    [0:age] => 21
    [1:name] => Jill
    [1:age] => 23
)

// Let's flatten another array on top
print_r( arr::flatten_assoc( array( array( "name" => "Humpty", "age" => 11 ) ) ,":",false) );

// Returns
Array
(
    [0:name] => Humpty
    [0:age] => 11
    [1:name] => Jill
    [1:age] => 23
)




</code></pre>
					</td>
				</tr>
				</tbody>
			</table>
		</article>

		<article>
			<h4>element($array, $key, $default = false)</h4>
			<p>The <strong>element</strong> method returns the element of the given array or a default if it is not set.</p>
			<table class="method">
				<tbody>
				<tr>
					<th class="legend">Static</th>
					<td>Yes</td>
				</tr>
				<tr>
					<th>Parameters</th>
					<td>
						<table class="parameters">
							<tr>
								<th>Param</th>
								<th>Default</th>
								<th class="description">Description</th>
							</tr>
							<tr>
								<th><kbd>$array</kbd></th>
								<td><i>Required</i></td>
								<td>The array to access</td>
							</tr>
							<tr>
								<th><kbd>$key</kbd></th>
								<td><i>Required</i></td>
								<td>The key requested in $array.</td>
							</tr>
							<tr>
								<th><kbd>$default</kbd></th>
								<td><pre class="php"><code>false</code></pre></td>
								<td>The value to be returned if the requested key does not exist</td>
							</tr>
						</table>
					</td>
				</tr>
				<tr>
					<th>Returns</th>
					<td>mixed</td>
				</tr>
				<tr>
					<th>Example</th>
					<td>
						<pre class="php"><code>$person = array(
	"name" => "Jack",
	"age" => "21",
	"location" => array(
		"city" => "Pittsburgh",
		"state" => "PA",
		"country" => "US"
	)		
);

echo arr::element($person, "name", "Unknown Name");
// Returns "Jack"

echo arr::element($person, "job", "Unknown job");
// Returns "Unknown job"

// This method can also dive into arrays by using a dot to separate between keys
echo arr::element($person, "location.city", "Unknown City");
// Returns "Pittsburgh"</code></pre>
					</td>
				</tr>
				</tbody>
			</table>
		</article>

		<article>
			<h4>insert(Array &$original, $value, $pos)</h4>
			<p>The <strong>insert</strong> method is mainly an array_splice alias with added error checking <br />
			<u>WARNING</u>: The original array is edited by reference, only boolean success is returned</p>
			<table class="method">
				<tbody>
				<tr>
					<th class="legend">Static</th>
					<td>Yes</td>
				</tr>
				<tr>
					<th>Parameters</th>
					<td>
						<table class="parameters">
							<tr>
								<th>Param</th>
								<th>Default</th>
								<th class="description">Description</th>
							</tr>
							<tr>
								<th><kbd>$original</kbd></th>
								<td><i>Required</i></td>
								<td>The array to use</td>
							</tr>
							<tr>
								<th><kbd>$value</kbd></th>
								<td><i>Required</i></td>
								<td>The value(s) to insert</td>
							</tr>
							<tr>
								<th><kbd>$pos</kbd></th>
								<td><i>Required</i></td>
								<td>The numeric position at which to insert, negative to count from the end backwards</td>
							</tr>
						</table>
					</td>
				</tr>
				<tr>
					<th>Returns</th>
					<td>boolean</td>
				</tr>
				<tr>
					<th>Example</th>
					<td>
						<pre class="php"><code>$people = array("Jack", "Jill");

// Add one value
arr::insert($people, "Humpty", 0 );
print_r( $people );
// Returns 
Array
(
    [0] => Humpty
    [1] => Jack
    [2] => Jill
)

// Add multiple values
arr::insert($people, array("Hansel", "Gretel"), 1 );
print_r( $people );
// Returns
Array
(
    [0] => Humpty
    [1] => Hansel
    [2] => Gretel
    [3] => Jack
    [4] => Jill
)

// Add an array
arr::insert($people, array( array("name" => "Wolf", "teeth" => "sharp" ) ), 0 );
print_r( $people );

// Returns
Array
(
    [0] => Array
        (
            [name] => Wolf
            [teeth] => sharp
        )

    [1] => Humpty
    [2] => Hansel
    [3] => Gretel
    [4] => Jack
    [5] => Jill
)

</code></pre>
					</td>
				</tr>
				</tbody>
			</table>
		</article>

<article>
			<h4>insert_after_key(Array &$original, $value, $key)</h4>
			<p>The <strong>insert_after_key</strong> method adds an element to an array after the key specified <br />
			<u>WARNING</u>: The original array is edited by reference, only boolean success is returned</p>
			<table class="method">
				<tbody>
				<tr>
					<th class="legend">Static</th>
					<td>Yes</td>
				</tr>
				<tr>
					<th>Parameters</th>
					<td>
						<table class="parameters">
							<tr>
								<th>Param</th>
								<th>Default</th>
								<th class="description">Description</th>
							</tr>
							<tr>
								<th><kbd>$original</kbd></th>
								<td><i>Required</i></td>
								<td>The array to use</td>
							</tr>
							<tr>
								<th><kbd>$value</kbd></th>
								<td><i>Required</i></td>
								<td>The value(s) to insert</td>
							</tr>
							<tr>
								<th><kbd>$key</kbd></th>
								<td><i>Required</i></td>
								<td>The key after which to insert</td>
							</tr>
						</table>
					</td>
				</tr>
				<tr>
					<th>Returns</th>
					<td>boolean</td>
				</tr>
				<tr>
					<th>Example</th>
					<td>
						<pre class="php"><code>$people = array("Jack", "Jill");

arr::insert_after_key($people, "Humpty", 1 );
print_r( $people );

// Returns
Array
(
    [0] => Jack
    [1] => Jill
    [2] => Humpty
)
</code></pre>
					</td>
				</tr>
				</tbody>
			</table>
		</article>

<article>
			<h4>insert_after_value(Array &$original, $value, $search)</h4>
			<p>The <strong>insert_after_value</strong> method adds an element to an array after the value specified <br />
			<u>WARNING</u>: The original array is edited by reference, only boolean success is returned</p>
			<table class="method">
				<tbody>
				<tr>
					<th class="legend">Static</th>
					<td>Yes</td>
				</tr>
				<tr>
					<th>Parameters</th>
					<td>
						<table class="parameters">
							<tr>
								<th>Param</th>
								<th>Default</th>
								<th class="description">Description</th>
							</tr>
							<tr>
								<th><kbd>$original</kbd></th>
								<td><i>Required</i></td>
								<td>The array to use</td>
							</tr>
							<tr>
								<th><kbd>$value</kbd></th>
								<td><i>Required</i></td>
								<td>The value(s) to insert</td>
							</tr>
							<tr>
								<th><kbd>$search</kbd></th>
								<td><i>Required</i></td>
								<td>The value after which to insert</td>
							</tr>
						</table>
					</td>
				</tr>
				<tr>
					<th>Returns</th>
					<td>boolean</td>
				</tr>
				<tr>
					<th>Example</th>
					<td>
						<pre class="php"><code>$people = array("Jack", "Jill");

arr::insert_after_key($people, "Humpty", "Jack" );
print_r( $people );

// Returns
Array
(
    [0] => Jack
    [1] => Humpty
    [2] => Jill
)
</code></pre>
					</td>
				</tr>
				</tbody>
			</table>
		</article>
		
		<article>
			<h4>sort($array, $key, $order = 'asc', $sort_flags = SORT_REGULAR)</h4>
			<p>The <strong>sort</strong> method sorts a multi-dimensional array by it's values.</p>
			<table class="method">
				<tbody>
				<tr>
					<th class="legend">Static</th>
					<td>Yes</td>
				</tr>
				<tr>
					<th>Parameters</th>
					<td>
						<table class="parameters">
							<tr>
								<th>Param</th>
								<th>Default</th>
								<th class="description">Description</th>
							</tr>
							<tr>
								<th><kbd>$array</kbd></th>
								<td><i>Required</i></td>
								<td>The array to sort</td>
							</tr>
							<tr>
								<th><kbd>$key</kbd></th>
								<td><i>Required</i></td>
								<td>The key requested to sort by in $array.</td>
							</tr>
							<tr>
								<th><kbd>$order</kbd></th>
								<td><pre class="php"><code>'asc'</code></pre></td>
								<td>The value to be returned if the requested key does not exist</td>
							</tr>
							<tr>
								<th><kbd>$sort_flags</kbd></th>
								<td><pre class="php"><code>SORT_REGULAR</code></pre></td>
								<td>The php sort type flags (<a href="http://php.net/manual/en/function.sort.php">http://php.net/manual/en/function.sort.php</a>)</td>
							</tr>
						</table>
					</td>
				</tr>
				<tr>
					<th>Returns</th>
					<td>mixed</td>
				</tr>
				<tr>
					<th>Example</th>
					<td>
						<pre class="php"><code>$data = array(
	array(
		'info' => array(
			'pet' => array(
				'type' => 'dog'
			)
		),
	),
	array(
		'info' => array(
			'pet' => array(
				'type' => 'fish'
			)
		),
	),
	array(
		'info' => array(
			'pet' => array(
				'type' => 'cat'
			)
		),
	),
);

$data = arr::sort($data, 'info.pet.type');
// Returns
array(
	array(
		'info' => array(
			'pet' => array(
				'type' => 'cat'
			)
		),
	),
	array(
		'info' => array(
			'pet' => array(
				'type' => 'dog'
			)
		),
	),
	array(
		'info' => array(
			'pet' => array(
				'type' => 'fish'
			)
		),
	),
);</code></pre>
					</td>
				</tr>
				</tbody>
			</table>
		</article>


	</section>

	<section id="footer">
		<p>
			<a href="http://fuelphp.com">Fuel PHP</a> is released under the MIT license.<br />
			&copy; 2010 - 2011 Fuel Development Team
		</p>
	</section>

</body>
</html>
